var pool = require("./config/db.js"),
    markdown = require('markdown').markdown;

/**
 * 房间
 * @param hotel
 * @constructor
 */
function Hotel(hotel){
    if(hotel){
        this.hotel_id=hotel.hotel_id;
        this.city_id=hotel.city_id;
        this.country_id=hotel.country_id;
        this.name=hotel.name;
        this.photo1=hotel.photo1;
        this.photo2=hotel.photo2;
        this.photo3=hotel.photo3;
        this.photo4=hotel.photo4;
        this.photo5=hotel.photo5;
        this.hotel_address=hotel.hotel_address;
        this.phone1=hotel.phone1;
        this.phone2=hotel.phone2;
        this.phone3=hotel.phone3;
        this.phone4=hotel.phone4;
        this.contact_email=hotel.contact_email;
        this.room_description=hotel.room_description;
        this.area_description=hotel.area_description;
        this.travel_description=hotel.travel_description;
        this.food_description=hotel.food_description;
        this.rates_added=hotel.rates_added;
    }
};

module.exports = Hotel;

/**
 * 增
 * @param callback
 */
Hotel.prototype.save = function(callback) {
    var _hotel = {
        city_id:this.city_id,
        country_id:this.country_id,
        name:this.name,
        photo1:this.photo1,
        photo2:this.photo2,
        photo3:this.photo3,
        photo4:this.photo4,
        photo5:this.photo5,
        hotel_address:this.hotel_address,
        phone1:this.phone1,
        phone2:this.phone2,
        phone3:this.phone3,
        phone4:this.phone4,
        contact_email:this.contact_email,
        room_description:this.room_description,
        area_description:this.area_description,
        travel_description:this.travel_description,
        food_description:this.food_description,
        rates_added:this.rates_added
    };
    pool.getConnection(function(err,connection){
        connection.query('INSERT INTO `hotel` SET ?',
            _hotel,
            function (err, rows, fields) {
                //写入系统日志
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 删一个
 * @param callback
 */
Hotel.prototype.delete = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('DELETE FROM `hotel` WHERE hotel_id=?',
            [_id],
            function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 改一个
 * @param callback
 */
Hotel.prototype.update = function(callback) {
    var _hotel = {
        hotel_id:this.hotel_id,
        city_id:this.city_id,
        country_id:this.country_id,
        name:this.name,
        photo1:this.photo1,
        photo2:this.photo2,
        photo3:this.photo3,
        photo4:this.photo4,
        photo5:this.photo5,
        hotel_address:this.hotel_address,
        phone1:this.phone1,
        phone2:this.phone2,
        phone3:this.phone3,
        phone4:this.phone4,
        contact_email:this.contact_email,
        room_description:this.room_description,
        area_description:this.area_description,
        travel_description:this.travel_description,
        food_description:this.food_description,
        rates_added:this.rates_added
    };
    for (var i in _hotel){
        if(!_hotel[i]){
            delete _hotel[i];
        }
    }
    pool.getConnection(function(err,connection){
        connection.query('UPDATE `hotel` SET ? WHERE hotel_id =?',
            [
                _hotel,
                _hotel.hotel_id
            ],
            function (err, rows, fields) {
                if (err) {
                    //throw err;
                    callback(true);
                };
                connection.release();
                callback(null,rows);
            });
    });
};

/**
 * 查一个
 * @param hotel_id callback
 */
Hotel.prototype.getOne = function(id,callback) {
    var _id= id;
    pool.getConnection(function(err,connection){
        connection.query('SELECT * from `hotel` WHERE hotel_id=?',
            [_id]
            , function (err, rows, fields) {
                if (err) throw err;
                connection.release();
                callback(null,rows);
            });
    });
};


/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
Hotel.getTen = function(pagesize,pages,callback) {
    var _size = pagesize ? pagesize  : 10,
        _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;

    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
            connection.query('SELECT COUNT(*) FROM hotel',function(err,rows,fields){
                _total = rows[0]['COUNT(*)'];
                _totalPage = _total / _size;
                _query();
            });
        }
        var _query = function(){
            connection.query('SELECT * from hotel ORDER BY `hotel_id` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows,_totalPage);
                });
        }
    });
};

/**
 * 分页查
 * @param pagesize 每页个数
 * @param pages    当前页
 * @param callback
 */
Hotel.schTen = function(pagesize,pages,CDbcriteria,callback) {
    var _criteria = (function(criteria){
        if(criteria){
            var _result = ' where name like "%'+criteria.name+'%" and country_id = '+criteria.country_id+'';
            return _result;
        }
    })(CDbcriteria);
    var _size = pagesize ? pagesize  : 10,
        _page = pages ? pages : 1,
        _total = null,
        _totalPage=null;
    pool.getConnection(function(err,connection){
        if(err){
            callback("连接池出错");
        }
        if(_total == null){
            connection.query('SELECT COUNT(*) FROM hotel' +_criteria,function(err,rows,fields){
                _total = rows[0]['COUNT(*)'];
                _totalPage = _total / _size;
                _query();
            });
        }
        var _query = function(){
            connection.query('SELECT * from hotel '+_criteria+' ORDER BY `hotel_id` DESC LIMIT ?,?',
                [_totalPage>=1 ? (_page-1)*_size : 0,
                    _totalPage <_page ? (_page*_size) :_size]
                , function (err, rows, fields) {
                    if (err){
                        callback("查询出错");
                    }
                    connection.release();
                    callback(null,rows,_totalPage);
                });
        }
    });
};